package com.tsj.protocol;

import com.tsj.common.Invocation;
import com.tsj.register.LocalRegister;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.reflect.Method;

/**
 * @Author tsj
 * @Date 2023/11/15/14:59
 */
public class HttpServerHandler {

    public void handler(HttpServletRequest req, HttpServletResponse resp) {
        // 处理请求 ---> 接口 ，方法， 方法参数

        try {
            Invocation  invocation = (Invocation) new ObjectInputStream(req.getInputStream()).readObject();
            String interfaceName = invocation.getInterfaceName();
            Class classImpl = LocalRegister.getClass(interfaceName,"1.0");
            Method method = classImpl.getMethod(invocation.getMethodName(), invocation.getParameterTypes());
            String result = (String) method.invoke(classImpl.newInstance(), invocation.getParameters());
            IOUtils.write(result,resp.getOutputStream());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }



    }
}
